package org.tmsps.ne4spring.base.dto;

import org.tmsps.ne4spring.base.ne.NePage;
import org.tmsps.ne4spring.base.ne.NeSort;
import org.tmsps.ne4spring.utils.ChkUtil;

import com.alibaba.fastjson.JSONObject;

import lombok.Data;

/**
 * element 的BaseQuery插件
 *
 * @author hxj
 */
@Data
public abstract class BaseEleQuery implements BaseQuery {

	private int limit = 20;
	private int page = 1;
	private String sorter;

	public NeSort getSort() {
		if (ChkUtil.isNotNull(sorter) && !"{}".equals(sorter.trim())) {
			JSONObject sorterJson = JSONObject.parseObject(sorter);
			String field = sorterJson.getString("field");
			String order = sorterJson.getString("order");
			if("descending".equals(order)) {
				order = "desc";
			}else {
				order = "asc";
			}

			NeSort neSort = new NeSort(field, order);
			return neSort;
		}
		return null;
	}

	public NePage getPage() {
		NePage nePage = new NePage();
		nePage.setPageNumber(page);
		nePage.setPageSize(limit);
		return nePage;
	}

}
